package com.pet.server.ext.action.home;


import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.baidu.gson.JsonObject;
import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.BeanToJson;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.Status;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.service.ArticleService;
import com.pet.server.ext.service.ProductService;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;
import com.pet.server.ext.util.baidu.DistanceEstimator;
import com.pet.server.ext.util.meituan.MeiTuanApi;
import com.pet.server.ext.util.meituan.XMLParse;
import com.pet.server.ext.util.meituan.XmlData;

public class Product extends ActionBase{

	public  String shipin_code="005002";
	public  String yongpin_code="005001";
	public  String fuwu_code="005003";
	public Integer startIndex,itemCount,radius;
	public String queryString,cate,city,district;
	public Double latitude,longitude;
	public Page page;
	public Product(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		cate = params.getFieldValue("cate");
		startIndex = NumberUtil.getInteger(params.getFieldValue("startIndex"), 0);
		itemCount =  NumberUtil.getInteger(params.getFieldValue("itemCount"), 10);
		radius = NumberUtil.getInteger(params.getFieldValue("radius"),10);
		latitude = NumberUtil.getDouble(params.getFieldValue("latitude"));
		longitude = NumberUtil.getDouble(params.getFieldValue("longitude"));
		city = params.getFieldValue("city");
		district = params.getFieldValue("district");
		queryString = params.getFieldValue("queryString");
		page = new Page(startIndex, itemCount,queryString);
	}
	
	@Override
	public Result invokeService() throws ApiError {
		JSONObject o = new JSONObject();
		ProductService productService = new ProductService();
		ArticleService articleService = new ArticleService();
		
		if("001003".equals(cate)){
			List<XmlData> list = null;
			String strs=null;
			if(!StringUtil.isEmpty(latitude,longitude)){
				strs = MeiTuanApi.callMeiTuanByDili(latitude, longitude, null, page.itemCount, null, page.startIndex, radius);
			}else{
				strs=MeiTuanApi.callMeiTuanByCity(strs, district, null, page.itemCount, null, page.startIndex);
			}
			list = XMLParse.parseXmlToList(strs);
			for(XmlData d:list){
				d.distance=DistanceEstimator.route(latitude, longitude, "", d.shop_lat, d.shop_long, "");
				d.source=Status.Source.meituan.toString();
			}
			
			page.list=list;//coverToProduct(list);
		}else{
			productService.searchProductInfo(page, cate);
		}
		//List<Constant> list = articleService.queryConstant(pinpai);
		List<Constant> fenlei = articleService.queryConstant(cate);
		//o.put("pinpai", BeanToJson.listTojson(list, "id","pcode","val"));
		for(Constant lei:fenlei){
			lei.pinpai=articleService.queryConstantByCate(lei.code);
			lei.list=articleService.queryConstant(lei.code);
		}
		o.put("fenlei", BeanToJson.listTojson(fenlei, "id","pcode","val"));
		Result result = new Result();
        result.setPage(o, page, "state");
        result.put("data", o);
	    return result;
	}

	
	/**
    public List<ProductInfo> coverToProduct(List<XmlData> list){
    	List<ProductInfo> l = new ArrayList<ProductInfo>();
    	for(XmlData d:list){
    		ProductInfo p = new ProductInfo(-1, d.deal_name, d.deal_img, d.deal_wap_url, "001003", "", "", "", "", "", null, d.value, d.price);
    		if(!StringUtil.isEmpty(latitude,longitude)){
    			double distance = DistanceEstimator.route(latitude, longitude, "", d.deal_lat, d.deal_lng, "");
    			p.distance=distance;
    		}
    		
    		l.add(p);
    	}
    	return l;
    }
    */
	
}
